#include "business_clock_page.h"
#include <time.h>

WeatherInfo currentWeather;  // 当前天气信息的实例

// 星期几的字符串数组
const char* weekDayStr(int wday) {
    static const char* weekdays[] = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
    return weekdays[wday];
}

// 主页面显示函数
void displayBusinessClockPage() {
    UBYTE *BlackImage = NULL, *RYImage = NULL;
    
    Serial.println("初始化墨水屏显示...");
    DEV_Module_Init();
    
    // 初始化并清除屏幕
    EPD_7IN5B_V2_Init();
    EPD_7IN5B_V2_Clear();
    DEV_Delay_ms(500);
    
    // 计算图像缓冲区大小
    UWORD Imagesize = ((EPD_7IN5B_V2_WIDTH % 8 == 0) ? (EPD_7IN5B_V2_WIDTH / 8) : (EPD_7IN5B_V2_WIDTH / 8 + 1)) * EPD_7IN5B_V2_HEIGHT;
    
    // 分配内存
    if ((BlackImage = (UBYTE *)malloc(Imagesize)) == NULL) {
        Serial.println("Failed to apply for black memory...");
        return;
    }
    
    if ((RYImage = (UBYTE *)malloc(Imagesize)) == NULL) {
        Serial.println("Failed to apply for red/yellow memory...");
        free(BlackImage);
        return;
    }
    
    // 创建新图像
    Paint_NewImage(BlackImage, EPD_7IN5B_V2_WIDTH, EPD_7IN5B_V2_HEIGHT, 0, WHITE);
    Paint_NewImage(RYImage, EPD_7IN5B_V2_WIDTH, EPD_7IN5B_V2_HEIGHT, 0, WHITE);
    
    // 清除缓冲区
    Paint_SelectImage(BlackImage);
    Paint_Clear(WHITE);
    Paint_SelectImage(RYImage);
    Paint_Clear(WHITE);
    
    // 打印实际屏幕尺寸
    Serial.print("屏幕实际宽度: ");
    Serial.println(EPD_7IN5B_V2_WIDTH);
    Serial.print("屏幕实际高度: ");
    Serial.println(EPD_7IN5B_V2_HEIGHT);
    
    // 绘制黑色图层内容
    Paint_SelectImage(BlackImage);
    
    // 绘制页面边框
    Paint_DrawRectangle(0, 0, EPD_7IN5B_V2_WIDTH-1, EPD_7IN5B_V2_HEIGHT-1, BLACK, DOT_PIXEL_1X1, DRAW_FILL_EMPTY);
    
    // 安全边距
    int safeMargin = 40;
    int contentWidth = EPD_7IN5B_V2_WIDTH - (safeMargin * 2);
    int contentHeight = EPD_7IN5B_V2_HEIGHT - (safeMargin * 2);
    
    // 位置信息区域
    Paint_DrawString_CN(safeMargin, safeMargin, "位", &Font12CN, BLACK, WHITE);
    if (currentWeather.city.length() > 0) {
        Paint_DrawString_CN(safeMargin, safeMargin + 20, currentWeather.city.c_str(), &Font12CN, BLACK, WHITE);
    } else {
        Paint_DrawString_CN(safeMargin, safeMargin + 20, "未知", &Font12CN, BLACK, WHITE);
    }
    
    // 时间信息区域 - 用已有的字符替代
    Paint_DrawString_CN(safeMargin, safeMargin + 60, "时", &Font12CN, BLACK, WHITE);
    // 获取并显示时间
    struct tm timeinfo;
    if (getLocalTime(&timeinfo)) {
        char timeStr[9];
        sprintf(timeStr, "%02d:%02d", timeinfo.tm_hour, timeinfo.tm_min);
        Paint_DrawString_EN(safeMargin, safeMargin + 80, timeStr, &Font24, BLACK, WHITE);
        
        char dateStr[20];
        sprintf(dateStr, "%d-%02d-%02d", timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday);
        Paint_DrawString_EN(safeMargin, safeMargin + 110, dateStr, &Font12, BLACK, WHITE);
        
        // 星期几 - 用数字表示
        char weekStr[3];
        sprintf(weekStr, "%d", timeinfo.tm_wday);
        Paint_DrawString_EN(safeMargin + 80, safeMargin + 110, weekStr, &Font12, BLACK, WHITE);
    } else {
        Paint_DrawString_EN(safeMargin, safeMargin + 80, "Error", &Font12, BLACK, WHITE);
    }
    
    // 温度信息区域
    Paint_DrawString_CN(safeMargin, safeMargin + 140, "温", &Font12CN, BLACK, WHITE);
    String tempText = String(currentWeather.temperature) + "C";
    Paint_DrawString_EN(safeMargin, safeMargin + 160, tempText.c_str(), &Font24, BLACK, WHITE);
    
    // 天气信息区域
    Paint_DrawString_CN(safeMargin, safeMargin + 200, "天", &Font12CN, BLACK, WHITE);
    Paint_DrawString_CN(safeMargin, safeMargin + 220, "雪", &Font24CN, BLACK, WHITE);  // 用"雪"字替代实际天气
    
    // 在红色图层上绘制图标
    Paint_SelectImage(RYImage);
    
    // 天气图标位置
    int iconX = EPD_7IN5B_V2_WIDTH - safeMargin - 100;
    int iconY = safeMargin + 80;
    int iconSize = 80;
    
    // 简单的天气图标
    if (currentWeather.weather.indexOf("晴") >= 0) {
        // 太阳图标
        Paint_DrawCircle(iconX + iconSize/2, iconY + iconSize/2, iconSize/3, BLACK, DOT_PIXEL_1X1, DRAW_FILL_FULL);
    } 
    else if (currentWeather.weather.indexOf("阴") >= 0 || currentWeather.weather.indexOf("云") >= 0) {
        // 云朵图标
        Paint_DrawCircle(iconX + iconSize/3, iconY + iconSize/3, iconSize/4, BLACK, DOT_PIXEL_1X1, DRAW_FILL_FULL);
        Paint_DrawCircle(iconX + iconSize*2/3, iconY + iconSize/3, iconSize/4, BLACK, DOT_PIXEL_1X1, DRAW_FILL_FULL);
        Paint_DrawCircle(iconX + iconSize/2, iconY + iconSize*2/3, iconSize/4, BLACK, DOT_PIXEL_1X1, DRAW_FILL_FULL);
    }
    else if (currentWeather.weather.indexOf("雨") >= 0) {
        // 雨天图标
        Paint_DrawCircle(iconX + iconSize/2, iconY + iconSize/3, iconSize/4, BLACK, DOT_PIXEL_1X1, DRAW_FILL_FULL);
        
        // 雨滴
        for (int i = 0; i < 3; i++) {
            int xOffset = iconX + iconSize/4 + i * (iconSize/4);
            Paint_DrawLine(xOffset, iconY + iconSize/2, xOffset, iconY + iconSize*3/4, BLACK, DOT_PIXEL_1X1, LINE_STYLE_SOLID);
        }
    }
    
    // WiFi状态指示
    if (WiFi.status() == WL_CONNECTED) {
        Paint_DrawString_EN(EPD_7IN5B_V2_WIDTH - safeMargin - 100, EPD_7IN5B_V2_HEIGHT - safeMargin - 20, "WiFi OK", &Font12, BLACK, WHITE);
    } else {
        Paint_DrawString_EN(EPD_7IN5B_V2_WIDTH - safeMargin - 100, EPD_7IN5B_V2_HEIGHT - safeMargin - 20, "WiFi ERROR", &Font12, BLACK, WHITE);
    }
    
    Serial.println("显示到屏幕...");
    // 显示内容
    EPD_7IN5B_V2_Display(BlackImage, RYImage);
    
    // 清理显示缓冲区
    free(BlackImage);
    free(RYImage);
    BlackImage = NULL;
    RYImage = NULL;
    
    // 延时以确保显示完成
    DEV_Delay_ms(2000);
    
    Serial.println("显示完成");
}

// 添加局部刷新时间的函数
void refreshTimeOnly() {
    // 初始化局部刷新
    EPD_7IN5B_V2_Init_Part();
    EPD_7IN5B_V2_Display_Base_color(WHITE);
    
    // 计算时间区域大小
    int timeWidth = 200;  // 时间显示区域宽度
    int timeHeight = 50;  // 时间显示区域高度
    
    // 区域坐标
    int timeAreaX = 40;  // 安全边距
    int timeAreaY = 40 + 80;  // 安全边距 + 位置区域高度
    
    UBYTE *TimeImage;
    UWORD TimeImageSize = ((timeWidth % 8 == 0) ? (timeWidth / 8) : (timeWidth / 8 + 1)) * timeHeight;
    
    // 分配内存
    if ((TimeImage = (UBYTE *)malloc(TimeImageSize)) == NULL) {
        Serial.println("Failed to apply for time refresh memory...");
        return;
    }
    
    // 创建时间区域图像
    Paint_NewImage(TimeImage, timeWidth, timeHeight, 0, WHITE);
    Paint_SelectImage(TimeImage);
    Paint_Clear(WHITE);
    
    // 获取并显示时间
    struct tm timeinfo;
    if (getLocalTime(&timeinfo)) {
        char timeStr[9];
        sprintf(timeStr, "%02d:%02d", timeinfo.tm_hour, timeinfo.tm_min);
        Paint_DrawString_EN(0, 5, timeStr, &Font24, BLACK, WHITE);
    } else {
        Paint_DrawString_EN(0, 5, "Error", &Font12, BLACK, WHITE);
    }
    
    // 局部刷新显示
    EPD_7IN5B_V2_Display_Partial(TimeImage, timeAreaX, timeAreaY, timeAreaX + timeWidth, timeAreaY + timeHeight);
    
    // 清理内存
    free(TimeImage);
    
    Serial.println("时间区域刷新完成");
}    